package com.baizhi.lql.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baizhi.lql.dao.VideoMapper;
import com.baizhi.lql.entity.Video;
import com.baizhi.lql.entity.VideoExample;
import com.baizhi.lql.utils.AliyunOSSUtil;
import com.baizhi.lql.utils.FileUploadConstance;
import com.baizhi.lql.vo.AppVideoVO;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("videoService")
@Transactional
public class VideoServiceImpl implements VideoService {
    @Autowired
    private VideoMapper videodao;

    @Override
    public void remove(int id) {
        videodao.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Video queryOne(int id) {
        return videodao.selectByPrimaryKey(id);
    }

    @Override
    public void modify(Video video) {
        videodao.updateByPrimaryKeySelective(video);
    }

    @Override
    // 往oss中上传头像，返回oss中的远程文件路径
    // 参数1 headImg：代表要上传的视频文件
    // 参数2 oldImg：旧视频
    // 往oss中上传头像，返回 oss中的远程文件路径
    public String uploadHeadImg(MultipartFile headImg,String oldVideo) throws IOException {
        // 原始文件名
        String originalFilename = headImg.getOriginalFilename();
        String fileName = new Date().getTime() + "-" + originalFilename;
        String objectName = FileUploadConstance.USER_HEADIMG_DIR + fileName;  // 不含bucket头像
        if(oldVideo != null){
            AliyunOSSUtil.deleteFile(FileUploadConstance.BUCKET,oldVideo);
        }
        // 如果是添加，直接上传视频
        AliyunOSSUtil.uploadFileBytes(FileUploadConstance.BUCKET,objectName,headImg);

        return FileUploadConstance.BUCKET_DOMAIN_NAME + objectName;
    }

    @Override
    public void add(Video video) {
        videodao.insertSelective(video);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<AppVideoVO> queryByReleaseTime() {
        List<AppVideoVO> appVideoVOS = videodao.selectByReleaseTime();
        for (AppVideoVO appVideoVO : appVideoVOS) {
            // 根据video_id 查询点赞数
            int count = videodao.selectLikeCount(appVideoVO.getId());
            appVideoVO.setLikeCount(count);
        }
        return appVideoVOS;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public Map<String, Object> queryAllPage(int page, int pageSize) {
        Map<String,Object> map = new HashMap<>();
        VideoExample example = new VideoExample();
        int offset = (page-1)*pageSize;
        RowBounds rowBounds = new RowBounds(offset, pageSize);
        List<Video> videos = videodao.selectByExampleAndRowBounds(example, rowBounds);
        int count = videodao.selectCount(null);

        map.put("page",page);
        map.put("rows",videos);
        map.put("total",count);

        return map;
    }
}
